/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/controls/VuMeter.cpp,v 1.4 2003/11/21 12:39:24 itsme Exp $
 *
 *
 * implements horizontal vumeter
 *
 * currently not used, - see discrete level vumeter class VuMeter2
 *
 */

#include "stdafx.h"
#include "resource.h"
#include "VuMeter.h"

#include "debug.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVuMeter

CVuMeter::CVuMeter()
{
}

CVuMeter::~CVuMeter()
{
}


BEGIN_MESSAGE_MAP(CVuMeter, CButton)
    //{{AFX_MSG_MAP(CVuMeter)
    ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVuMeter message handlers

void CVuMeter::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
    CBrush br;
    COLORREF black= 0x00000000;
    COLORREF red= 0x000000ff;

    CDC dc; dc.Attach(lpDrawItemStruct->hDC);

    RECT rleft= lpDrawItemStruct->rcItem; rleft.right= rleft.left+m_npixels;
    RECT rright= lpDrawItemStruct->rcItem;rright.left= rleft.right;

    dc.FillSolidRect(&rleft, red);
    dc.FillSolidRect(&rright, black);
}

BOOL CVuMeter::Create(const RECT& rect, CWnd* pParentWnd, UINT nID, bool bEnabled, int min, int max)
{
    m_min= min;
    m_max= max;
    m_level= m_min-1;
    // !! CE does not support BS_BITMAP -> use BS_OWNERDRAW
    return CWnd::Create(L"BUTTON", L"", WS_CHILD|WS_VISIBLE|BS_OWNERDRAW|(bEnabled?0:WS_DISABLED) , rect, pParentWnd, nID);
}

void CVuMeter::SetRange(int min, int max)
{
    m_min= min;
    m_max= max;
}

void CVuMeter::SetLevel(int level)
{
    if (level==m_level)
        return;

    if (level<m_min)
        m_level= m_min;
    else if (level>m_max)
        m_level= m_max;
    else
        m_level= level;

    RECT wr;
    GetWindowRect(&wr);

    m_npixels= (int)((double)(wr.right-wr.left)*(level-m_min)/(m_max-m_min)+0.5);

    this->RedrawWindow();
}

void CVuMeter::OnClicked() 
{
    // -- ignoring click
    
}
